import react from '@vitejs/plugin-react-swc'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import type { ConfigEnv } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import UnoCSS from 'unocss/vite'
import svgr from 'vite-plugin-svgr';
import { visualizer } from "rollup-plugin-visualizer"
import compression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv) => {
  const currentEnv = loadEnv(mode, process.cwd())
  console.log('Current mode:', command)
  console.log('Current environment configuration:', currentEnv) //loadEnv即加载根目录下.env.[mode]环境配置文件
  return defineConfig({
    plugins: [
      compression({
        verbose: true, // 是否在控制台输出压缩结果
        disable: false, // 是否禁用压缩
        algorithm: 'gzip', // 压缩算法
        threshold: 10240, // 压缩阈值，小于该大小的文件将不会被压缩
      }),
      react(),
      UnoCSS(),
      visualizer(),
      AutoImport({
        imports: ['react', 'react-router-dom',
          {
            'styled-components': [
              ['default', 'styled'],
              'createGlobalStyle',
              'keyframes'
            ],
          }
        ],
        dts: './src/auto-imports.d.ts',
        dirs: ['src/store'],
        eslintrc: {
          enabled: true, // Default `false`
          filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
        },
      }),
      svgr()
    ],
    //项目部署的基础路径,
    base: currentEnv.VITE_PUBLIC_PATH,
    mode: mode,
    resolve: {
      //别名
      alias: {
        '@': resolve(__dirname, './src'),
        '@components': resolve(__dirname, './src/components'),
        '@store': resolve(__dirname, './src/store'),
        '@views': resolve(__dirname, './src/views'),
        '@assets': resolve(__dirname, './src/assets'),
        '@hooks': resolve(__dirname, './src/hooks'),
      },
    },
    //服务
    server: {
      //自定义代理---解决跨域
      proxy: {
        // 选项写法
        // '/proxy': {
        //   target: 'http://100.65.0.80',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/proxy/, ''),
        // },
      },
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        sass: {},
      },
    },
    //构建
    build: {
      assetsInlineLimit: 100 * 1024,//100K 以内使用内联
      outDir: mode === 'docker' ? 'dist' : 'docs', //输出路径
      //构建后是否生成 source map 文件
      sourcemap: mode != 'production',
      rollupOptions: {
        output: {
          manualChunks: {
            'vendor': ['react', 'react-dom'],
            'router': ['react-router-dom'],
            'semi': ['@douyinfe/semi-ui', '@douyinfe/semi-icons', '@douyinfe/semi-icons-lab'],
            'library': ['axios', 'framer-motion', 'immer', 'zustand', 'styled-components', 'vconsole', 'normalize.css'],
          },
        },
      },
      //打包去掉打印信息 保留debugger vite3需要单独安装terser才行
      // minify: 'terser',
      // terserOptions: {
      //   compress: {
      //     drop_console: true,
      //     drop_debugger: false,
      //   },
      // },
    },
  })
}
